package Demo5;

import java.util.Arrays;
import java.util.Comparator;

class Student /*implements Comparable<Student>*/{   //引一个比较方法 ，常规比较
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
//    普通比较
//    @Override
//    public int compareTo(Student o) {   //写Comparable比较方法，返回整形
//        return this.age-o.age;//是谁调用的compareTo，this就是指的谁，o是指向参数的
//    } //一般就写上 可以共存，就当默认比较
}
class AgeComperator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}

class ScoreComperator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.score-o2.score;
    }
}

class NameComperator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("yzh",12,100);
        Student student2 = new Student("hhh",11,110);
        Student[] students = new Student[2];
        students[0] = student1;
        students[1] = student2;
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
        ScoreComperator scoreComperator = new ScoreComperator();
        Arrays.sort(students,scoreComperator);//按分排序
        System.out.println(Arrays.toString(students));
        Arrays.sort(students,new NameComperator());//按名字排序,直接new不用单独实例化
        System.out.println(Arrays.toString(students));

    }
    public static void main1(String[] args) {
        Student student1 = new Student("yzh",12,100);
        Student student2 = new Student("hhh",11,90);
        //比较对象的大小
//        if (student1.compareTo(student2)>0){
//            System.out.println("1>2的年龄");
//        }
//        Student[] students = new Student[2];
//        students[0] = student1;
//        students[1] = student2;
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));
        ScoreComperator scoreComperator = new ScoreComperator();
        int ret = scoreComperator.compare(student1,student2);
        System.out.println(ret);
    }
}
